package net.zoneland.knowledge.utils;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;

public class UrlUtil {

    private static PathMatcher pathMatcher = new AntPathMatcher();

    public static boolean pathMatchesUrl(List<String> paths, String url) {
        boolean flag = false;
        if (paths != null && !paths.isEmpty() && StringUtils.isNotEmpty(url)) {
            int firstQuestionMarkIndex = url.indexOf("?");
            if (firstQuestionMarkIndex != -1) {
                url = url.substring(0, firstQuestionMarkIndex);
            }
            for (String path : paths) {
                flag = pathMatcher.match(path, url);
                if (flag) {
                    break;
                }
            }
        }
        return flag;
    }

    public static boolean pathMatchesUrl(String path, String url) {
        boolean flag = false;
        if (StringUtils.isNotEmpty(url)) {
            int firstQuestionMarkIndex = url.indexOf("?");
            if (firstQuestionMarkIndex != -1) {
                url = url.substring(0, firstQuestionMarkIndex);
            }
            flag = pathMatcher.match(path, url);
        }
        return flag;
    }

    /**
     * Obtains the web application-specific fragment of the request URL.
     * <p>
     * Under normal spec conditions,
     * 
     * <pre>
     * requestURI = contextPath + servletPath + pathInfo
     * </pre>
     *
     * But the requestURI is not decoded, whereas the servletPath and pathInfo are
     * (SEC-1255). This method is typically used to return a URL for matching
     * against secured paths, hence the decoded form is used in preference to the
     * requestURI for building the returned value. But this method may also be
     * called using dummy request objects which just have the requestURI and
     * contextPatth set, for example, so it will fall back to using those.
     *
     * @return the decoded URL, excluding any server name, context path or servlet
     *         path
     *
     */
    public static String buildRequestUrl(HttpServletRequest req) {
        return buildRequestUrl(req.getServletPath(), req.getRequestURI(), req.getContextPath(), req.getPathInfo(),
                req.getQueryString());
    }

    /**
     * Obtains the web application-specific fragment of the URL.
     */
    public static String buildRequestUrl(String servletPath, String requestURI, String contextPath, String pathInfo,
            String queryString) {

        StringBuilder url = new StringBuilder();
        if (servletPath != null) {
            url.append(servletPath);
            if (pathInfo != null) {
                url.append(pathInfo);
            }
        } else {
            url.append(requestURI.substring(contextPath.length()));
        }

        if (queryString != null) {
            url.append("?").append(queryString);
        }

        return url.toString();
    }
}
